﻿using System.Linq;
using Inscriptionem.Models.Blog;
using Inscriptionem.Models.Blog.Enums;

namespace Inscriptionem.ORM.Extensions
{
    public static class PageExtensions
    {
        public static Article ToArticle(this Page page)
        {
            var comments = page.Comments.Where(c => (c.IsPublished && !c.IsDeleted) || (Authorization.Member.Current.ID != 0 && (!c.IsDeleted && c.Member.ID == Authorization.Member.Current.ID)));

            var article = new Article()
            {
                ID = page.ID,
                Type = ArticleType.Page,
                Title = page.Title,
                Content = page.Content,
                IsCommentable = page.IsCommentable,
                IsSectionPinned = page.IsSectionPinned,
                ViewCount = page.ViewCount,
                CreatedDate = page.CreatedDate,
                CommentsCount = comments.Count(),
            };

            return article;
        }
    }
}